<?php
namespace App\Http\Controllers\SysAdmin;

use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\User;
use App\Models\RoleUser;
use Illuminate\Support\Facades\Log;


class GroupController extends BaseController
{
    protected $role;


    public function __construct() {
        parent::__construct();
        $this->role = RoleUser::where('user_id',$this->user->id)->where('corpid',$this->corpid)->whereIn('role_id',['9'])->get();
        if(count($this->role) ==0){
            abort(403,'您没有权限访问该页面');
        }
    }


    //首页
    public function getIndex(Request $request)
    {
        $grp = Group::where('corpid',$this->corpid)->select('id','group_name','fid','class_id')->get();
        $grp = $grp->toArray();
        $groups = array();
        foreach ($grp as &$g){
            if($g['fid'] == 0){
                $groups[] = $g;
            }
        }
        foreach ($groups as $k=>&$t){
            if($t['id'] !=0){
                $t['child'] = $this->filterGroup($t['id'],$grp);
                foreach ($t['child'] as &$cld){
                    $cld['child'] = $this->filterGroup($cld['id'],$grp);
                }
            }
        }
        //dd($groups);
        if(is_mobile()){
            $view = 'sysadmin.group-manage';
        }else{
            $view = 'pc_sysadmin.group-manage';
        }
        return view($view)->with('groups',$groups);
    }

    //筛选组
    private function filterGroup($group_id,$groups){
        $return = array();
        foreach ($groups as $u){
            if($u['fid'] == $group_id){
                $return[] = $u;
            }
        }
        return $return;
    }

    //分组成员管理的通讯录页
    public function getSetMember(Request $request)
    {
        $group_id = $request->input('groupid',0);
        if($group_id <= 0 ){
            return '组名id无效';
        }
        $group = Group::where('corpid',$this->corpid)->find($group_id);
        if(empty($group)){
            return '没有找到对应组记录';
        }
        $users = User::where('corpid',$this->corpid)->where('status','1')->get();
        foreach ($users as $u){
            if($u->group_id == $group_id){
                $u->is_checked = 1;
            }else{
                $u->is_checked = 0;
            }
        }
        if(is_mobile()){
            $view = 'sysadmin.pick-colleague';
        }else{
            $view = 'pc_sysadmin.pick-colleague';
        }
        return view($view)
            ->with('groupid',$group_id)
            ->with('users',$users);
    }

    /*//首页
    public function getAddIndex(Request $request)
    {
        $view = 'sysadmin.delete-manage';
        return view($view);
    }

    //首页
    public function getAddColl(Request $request)
    {
        $view = 'sysadmin.add-newcolleague';
        return view($view);
    }*/



    //增加分组
    public function postAddGroup(Request $request)
    {
        $result = array(
            'status'        => 'success',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $group_name = $request->input('name','');
        if(empty($group_name)){
            $result['status'] = 'fail';
            $result['msg'] = '组名不能为空';
            return Response::json($result);
        }
        $fid = $request->input('fid',0);
        if($fid <0){
            $result['status'] = 'fail';
            $result['msg'] = 'fid不合法';
            return Response::json($result);
        }
        if($fid > 0){
            $fgroup = Group::find($fid);
            if(empty($fgroup)){
                $result['status'] = 'fail';
                $result['msg'] = '上级分组不存在';
                return Response::json($result);
            }
        }

        $group = new Group();
        $group->corpid = $this->corpid;
        $group->group_name = $group_name;
        $group->fid = $fid;
        $group->class_id = isset($fgroup->class_id)?$fgroup->class_id+1:1;
        $group->save();

        $result['data'] = $group;
        return Response::json($result);
    }

    //修改分组名称
    public function postModifyGroup(Request $request)
    {
        $result = array(
            'status'        => 'success',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $groupid = $request->input('groupid',0);
        $group_name = $request->input('name','');
        if(empty($group_name) || $groupid <=0 || strlen($group_name)>10){
            $result['status'] = 'fail';
            $result['msg'] = '组名不能为空或者id有误或者组名长度不符要求';
            return Response::json($result);
        }
        

        $group =  Group::find($groupid);
        if($group->corpid != $this->corpid){
            $result['status'] = 'fail';
            $result['msg'] = '您没有权限修改该组';
            return Response::json($result);
        }
        $group->group_name = $group_name;
        $group->save();

        $result['data'] = $group;
        $result['msg'] = '修改分组信息成功';
        return Response::json($result);
    }

    //删除分组
    public function postDelGroup(Request $request)
    {
        $result = array(
            'status'        => 'success',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $groupid = $request->input('groupid',0);
        if($groupid <= 0){
            $result['status'] = 'fail';
            $result['msg'] = '组id有误';
            return Response::json($result);
        }
        $group = Group::find($groupid);
        if(empty($group)){
            $result['status'] = 'fail';
            $result['msg'] = '分组不存在';
            return Response::json($result);
        }
        $child_groups = Group::where('corpid',$this->corpid)->where('fid',$group->id)->get();
        if(count($child_groups) > 0){
            $result['status'] = 'fail';
            $result['msg'] = '请先删除该组的子分组';
            return Response::json($result);
        }
        if($group->corpid != $this->corpid){
            $result['status'] = 'fail';
            $result['msg'] = '您没有权限删除该组';
            return Response::json($result);
        }
        //该组用户group_id重置为0
        $users = User::where('corpid',$this->corpid)->where('status','1')->where('group_id',$groupid)->get();
        foreach ($users as $u){
            $u->group_id = 0;
            $u->save();
        }
        $group->delete();


        $result['data'] = $group;
        return Response::json($result);
    }


    //分组成员管理
    public function postSetMember(Request $request)
    {
        $result = array(
            'status'        => 'fail',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $group_id = $request->input('groupid',0);
        $user_ids = $request->input('userid',0);
        $user_ids = json_decode($user_ids);

        if($group_id <=0  || count($user_ids)==0){
            return '传递参数错误';
        }

        $group = Group::find($group_id);
        if($group->corpid != $this->corpid){
            $result['msg'] = '您没有权限操作该分组';
            return Response::json($result);
        }

        //原组长不变,新进员工全部为组员
        $old_leader = User::where('corpid',$this->corpid)->where('status','1')->where('group_id',$group_id)->where('is_leader','1')->first();
        $guser = User::where('corpid',$this->corpid)->where('group_id',$group_id)->get();
        foreach ($guser as $gu){
            $gu->group_id = 0;
            $gu->save();
        }
        $users = User::where('corpid',$this->corpid)->where('status','1')->whereIn('id',$user_ids)->get();
        foreach ($users as $u){
            $u->group_id = $group_id;
            if(!empty($old_leader)){
                if($old_leader->id != $u->id){
                    $u->is_leader = 0;
                }
            }
            $u->save();
        }

        $result['status'] = 'success';
        $result['msg'] = '修改分组成员成功';
        return Response::json($result);
    }



   
}


